package com.zhr.liquwang.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.zhr.liquwang.R;
import com.zhr.liquwang.api.UrlManager;
import com.zhr.liquwang.entity.Goods;
import com.zhr.liquwang.entity.HomeBanner;
import com.zhr.liquwang.entity.XianShi;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

/**
 * .::::.
 * .::::::::.
 * :::::::::::
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 * Created by 毛懿 on 2016/11/10 10:08.
 * 邮箱：1334964028@qq.com
 */

public class HomeAdapter extends RecyclerView.Adapter {
    public static final int TYPE_HEAD = -1;
    public static final int TYPE_ITEM = -2;
    private Context context;
    private List<HomeBanner> banners;
    private List<XianShi> xianShis;
    private List<Goods.ResultBean> data;
    private int pageIndex = 1;
    private HomeGoodsClick goodsClick;

    public HomeAdapter(Context context, List<HomeBanner> banners, List<XianShi> xianShis, List<Goods.ResultBean> data, HomeGoodsClick goodsClick) {
        this.context = context;
        this.banners = banners;
        this.xianShis = xianShis;
        this.data = data;
        this.goodsClick = goodsClick;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEAD:
                return new HeadViewHolder(LayoutInflater.from(context).inflate(R.layout.item_home_head, parent, false));
            case TYPE_ITEM:
                return new ItemViewHolder(LayoutInflater.from(context).inflate(R.layout.item_home, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            HeadViewHolder holder1 = (HeadViewHolder) holder;
            holder1.bannerHome.setPages(new CBViewHolderCreator<LocalImageHolderView>() {

                @Override
                public LocalImageHolderView createHolder() {
                    return new LocalImageHolderView();
                }
            }, banners).stopTurning();
            holder1.bannerHome.startTurning(2000);
            holder1.bannerHome.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    goodsClick.click(Integer.valueOf(banners.get(position).getUrlid()));
                }
            });
            Glide.with(context).load(xianShis.get(0).getImageUrl()).into(holder1.ivXianshi1);
            Glide.with(context).load(xianShis.get(1).getImageUrl()).into(holder1.ivXianshi2);
            Glide.with(context).load(xianShis.get(2).getImageUrl()).into(holder1.ivXianshi3);
        } else {
            ItemViewHolder holder2 = (ItemViewHolder) holder;
            holder2.tvTitle.setText(data.get(position - 1).getHomeSort().getName());
            Glide.with(context).load(data.get(position - 1).getValue().get(0).getImageUrl()).into(holder2.iv1);
            Glide.with(context).load(data.get(position - 1).getValue().get(1).getImageUrl()).into(holder2.iv2);
            Glide.with(context).load(data.get(position - 1).getValue().get(2).getImageUrl()).into(holder2.iv3);
            Glide.with(context).load(data.get(position - 1).getValue().get(3).getImageUrl()).into(holder2.iv4);
            Glide.with(context).load(data.get(position - 1).getValue().get(4).getImageUrl()).into(holder2.iv5);
            Glide.with(context).load(data.get(position - 1).getValue().get(5).getImageUrl()).into(holder2.iv6);

            if (position == getItemCount() - 1) {
                if (pageIndex < 5) {
                    pageIndex++;
                    getMore();
                } else {

                }
            }
        }
    }

    public void getMore() {
        RequestParams params = new RequestParams();
        params.addFormDataPart("pageindex", pageIndex);
        params.addFormDataPart("pagesize", 3);
        HttpRequest.post(UrlManager.HOME_GOODS_URL, params, new BaseHttpRequestCallback<Goods>() {
            @Override
            protected void onSuccess(Goods goods) {
                super.onSuccess(goods);
                data.addAll(goods.getResult());
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        if (banners.size() == 0 || xianShis.size() == 0) {
            return 0;
        }
        return data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        } else {
            return TYPE_ITEM;
        }
    }

    public class HeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.banner_home)
        ConvenientBanner bannerHome;
        @BindView(R.id.tv_home_fl1)
        TextView tvHomeFl1;
        @BindView(R.id.tv_home_fl2)
        TextView tvHomeFl2;
        @BindView(R.id.tv_home_fl3)
        TextView tvHomeFl3;
        @BindView(R.id.tv_home_fl4)
        TextView tvHomeFl4;
        @BindView(R.id.tv_home_fl5)
        TextView tvHomeFl5;
        @BindView(R.id.tv_home_fl6)
        TextView tvHomeFl6;
        @BindView(R.id.tv_home_fl7)
        TextView tvHomeFl7;
        @BindView(R.id.tv_home_fl8)
        TextView tvHomeFl8;
        @BindView(R.id.tv_home_fl9)
        TextView tvHomeFl9;
        @BindView(R.id.tv_home_fl10)
        TextView tvHomeFl10;
        @BindView(R.id.iv_home_index1)
        ImageView ivHomeIndex1;
        @BindView(R.id.iv_home_index2)
        ImageView ivHomeIndex2;
        @BindView(R.id.iv_xianshi1)
        ImageView ivXianshi1;
        @BindView(R.id.iv_xianshi2)
        ImageView ivXianshi2;
        @BindView(R.id.iv_xianshi3)
        ImageView ivXianshi3;

        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_home_item)
        TextView tvTitle;
        @BindView(R.id.iv_home_item1)
        ImageView iv1;
        @BindView(R.id.iv_home_item2)
        ImageView iv2;
        @BindView(R.id.iv_home_item3)
        ImageView iv3;
        @BindView(R.id.iv_home_item4)
        ImageView iv4;
        @BindView(R.id.iv_home_item5)
        ImageView iv5;
        @BindView(R.id.iv_home_item6)
        ImageView iv6;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class LocalImageHolderView implements Holder<HomeBanner> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, HomeBanner data) {
            Glide.with(context).load(data.getImageUrl()).into(imageView);
        }
    }

    public interface HomeGoodsClick {
        void click(int shopId);
    }

}
